package com.huisang.service.user;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.huisang.dao.BaseDao;
import com.huisang.dao.user.UserDao;
import com.huisang.dao.user.UserDaoImpl;
import com.huisang.pojo.User;



public class UserServiceImpl implements UserService{

    //业务层调用dao层，引入dao层
    private UserDao userDao;
    public UserServiceImpl(){
        userDao=new UserDaoImpl();
    }
    //登录业务实现
    @Override
    public User login(String userCode,String userPassword) {
        Connection connection=null;
        User user=null;
        try {
            connection=BaseDao.getConnection();
            //通过业务层调用对应的具体的数据库操作
            user=userDao.getLoginUser(connection, userCode,userPassword);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return user;
    }
    //根据用户id修改密码
    @Override
    public boolean updatePwd(int id, String userPassword) {
        Connection connection=null;
        boolean flag=false;
        try {
            //获得数据库连接对象
            connection=BaseDao.getConnection();
            //修改密码调用dao层
            if(userDao.updatePwd(connection, id, userPassword)>0){
                flag=true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
        
    }
    //查询记录数
    @Override
    public int getUserCount(String userName, int userRole) {
        Connection connection=null;
        int count=0;
        try {
            connection=BaseDao.getConnection(); 
            count=userDao.getUserCount(connection, userName, userRole);

        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return count;
    }
    //根据条件查询用户列表
    @Override
    public List<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {
        Connection connection = null;
        List<User> userList = null;
        try {
            connection = BaseDao.getConnection();
            userList = userDao.getUserList(connection, queryUserName, queryUserRole, currentPageNo, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return userList;
    }
    //查询当前用户是否存在
    @Override
    public boolean boUser(String queryUserCode) {
        Connection connection = null;
        boolean flag=false;
        try {
            connection=BaseDao.getConnection();
            flag=userDao.boUser(connection, queryUserCode);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }
    //添加用户
    @Override
    public boolean add(User user) {
        boolean flag = false;
        Connection connection = null;
        
        int addNum;
        try {
            connection=BaseDao.getConnection();
            connection.setAutoCommit(false);//开启JDBC事务管理
            addNum = userDao.add(connection, user);
            connection.commit();
            if (addNum > 0) {
            flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();//事务回滚
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        } finally {
            //在service层进行connection连接的关闭
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }
    //根据用户id找到用户
    @Override
    public User getUserById(String id) {
        User user = null;
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            user = userDao.getUserById(connection, id);
        } catch (Exception e) {
            e.printStackTrace();
            user = null;
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return user;
    }
    //修改用户信息
    @Override
    public boolean modify(User user) {
        Connection connection = null;
        boolean flag = false;
        try {
            connection = BaseDao.getConnection();
            if (userDao.modify(connection, user) > 0) {
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }
    //删除用户信息
    @Override
    public boolean deleteUserById(Integer delId) {
        Connection connection = null;
        boolean flag = false;
        try {
            connection = BaseDao.getConnection();
            if (userDao.deleteUserById(connection, delId) > 0) {
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }

}